# -*- coding: utf-8 -*-
"""
@Time： 2023/12/20 16:20
@Auth： gubei
@File： init.py
@IDE：PyCharm
@Description：windows下初始化项目
"""
import warnings
from pathlib import Path as PyPath

from fastapi import FastAPI
from starlette.staticfiles import StaticFiles
from tortoise.contrib.fastapi import register_tortoise

from core.db.database import engine
from core.log import logger
from core.middlerwares.middleware import middleware
from core.models import models
from core.router import v1
from core.timer import scheduler
from env import setting

from utils.get_local_ip import local_ip
from utils.use_loacl_static import use_local_static


# 在 on_after_configure 事件中设置全局的 OAuthFlowsAuthorizationCode


class InitializeApp(object):
    """
    注册App
    """

    current_project_path = PyPath(__file__).resolve().parent.parent  # 获取当前项目根目录路径
    static_templates_path = current_project_path / "templates" / "static"  # 拼接路径

    def __new__(cls, *args, **kwargs):
        # 屏蔽警告

        # logger.bind(name=None).success(BANNER)

        app = FastAPI(title=setting.APP_TITLE, description=setting.APP_DESCRIPTION, middleware=middleware)
        app.mount("/static", StaticFiles(directory=cls.static_templates_path), name="static")

        cls.event_init(app)
        cls.register_router(app)
        return app

    @staticmethod
    def register_router(app: FastAPI) -> None:
        """
        注册路由
        :param app:
        :return:
        """
        # 项目API
        app.include_router(
            v1.ApiRouter(),
        )

    @staticmethod
    def event_init(app: FastAPI) -> None:
        """
        事件初始化
        :param app:
        :return:
        """

        @app.on_event("startup")
        async def startup():
            logger.bind(name=None).opt(ansi=True).success(f"Project is running at <red> DEBUG </red>")

            # 屏蔽警告
            warnings.filterwarnings("ignore", category=Warning)

            # 使用本地静态资源
            use_local_static()

            # 注册tortoise
            # register_tortoise(
            #     app,
            #     config=TORTOISE_ORM,
            #     generate_schemas=True,  # 重启服务时自动生成数据库表；关闭，改为使用aerich
            #     add_exception_handlers=True
            # )

            # 在数据库中生成表结构
            models.Base.metadata.create_all(bind=engine)

            scheduler.start()  # 定时任务
            logger.info(f"项目启动成功，文档地址：http://{local_ip}:{setting.APP_PORT}/docs")

        @app.on_event('shutdown')
        async def shutdown():
            """
            关闭
            :return:
            """
            scheduler.shutdown()
